﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntreeServer.Module;
using ME.SDK.SampleApplication.ControlTemplates;

namespace ME.SDK.SampleApplication
{
    public class ListSearchAppConfigPart : EntreeServer.Module.Configuration.EntreeConfigPart
    {
        public ListSearchAppConfigPart(EntreeApplication applicationToConfig)
            : base(applicationToConfig)
        {
               
        }

        /// <summary>
        /// DisplayName will be used as the title of the tab for this EntreeConfigPart on the config page.
        /// </summary>
        public override string DisplayName
        {
            get
            {
                return "List Search App Config";
            }
        }

        private const string ControlId = "ListSearchAppConfigPartTemplate";

        protected override void CreateChildControls()
        {
            //load the control template
            base.CreateChildControls();
            var c = Page.LoadControl("/_controltemplates/ME.SDK.SampleApplication/ListSearchAppConfigPartTemplate.ascx");
            //set the id for easy retrieval
            c.ID = ControlId;
            Controls.Add(c);
        }

        protected override void OnLoad(EventArgs e)
        {
            //get or create config
            var app = this.ModuleToConfig as ListSearchApp;
            var control = this.FindControl<ListSearchAppConfigPartTemplate>(ControlId);
            var config = new ListSearchAppConfig()
            {
                FieldInternalNames = new List<string>()
            };
            if (app != null && app.Config != null)
            {
                config = app.Config;
            }
            control.Config = config;
        }
        
        /// <summary>
        /// ApplyChanges is called when the 'Save' button is clicked.
        /// </summary>
        public override void ApplyChanges()
        {
            //retrieve control using id
            var control = this.FindControl<ListSearchAppConfigPartTemplate>(ControlId);
            control.ApplyChanges();
            var app = this.ModuleToConfig as ListSearchApp;
            if (app != null) app.Config = control.Config;
        }

    }
}
